﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Globalization;
using Libs.Partner;

public partial class Pages_Partners_Partner_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AppUtils.CheckLogin();
        Title = Title + " - Cập nhật đối tác";
        if (IsPostBack) return;
        init();
    }

    private void init()
    {
        // DropDownList
        var _Company = new Company();
        drpCompany.DataSource = _Company.GetTList();
        drpCompany.DataBind();
        drpCompany.Items.Insert(0, new ListItem("Chọn cơ quan, đơn vị:", "0"));

        // init
        var _Partner = new Partner() { PartnerID = AppUtils.Request("id") };
        _Partner = _Partner.Get();
        if (_Partner == null) Response.Redirect(Constant.ADMIN_PATH + Resources.Url.PartnerList);

        txtFullName.Text = _Partner.FullName;
        drpCompany.SelectedValue = _Partner.CompanyID.ToString();
        txtPosition.Text = _Partner.Position;
        txtEmail.Text = _Partner.Email;
        txtFax.Text = _Partner.Fax;
        txtMobile.Text = _Partner.Mobile;
        txtWorkPhone.Text = _Partner.WorkPhone;
        txtHomePhone.Text = _Partner.HomePhone;
        txtHomeAddress.Text = _Partner.HomeAddress;
        txtWorkAddress.Text = _Partner.WorkAddress;
        txtWebsite.Text = _Partner.Website;
        txtDescription.Text = _Partner.Description;

        if (_Partner.Birthday != DateTime.MinValue)
        {
            txtBirthday.Text = _Partner.Birthday.ToString("dd/MM/yyyy");
        }
    }

    protected void btUpdate_Click(object sender, EventArgs e)
    {
        var _Partner = new Partner() { PartnerID = AppUtils.Request("id") };
        _Partner = _Partner.Get();

        _Partner.FullName = txtFullName.Text.Trim();
        _Partner.CompanyID = Convert.ToInt32(drpCompany.SelectedValue);
        _Partner.Position = txtPosition.Text;
        _Partner.Email = txtEmail.Text.Trim().ToLower();
        _Partner.Fax = txtFax.Text.Trim();
        _Partner.Mobile = txtMobile.Text.Trim();
        _Partner.WorkPhone = txtWorkPhone.Text.Trim();
        _Partner.HomePhone = txtHomePhone.Text.Trim();
        _Partner.HomeAddress = txtHomeAddress.Text.Trim();
        _Partner.WorkAddress = txtWorkAddress.Text.Trim();
        _Partner.Website = txtWebsite.Text.Trim().ToLower();
        _Partner.Description = txtDescription.Text.Trim();

        if (txtBirthday.Text.Trim().Length > 0)
        {
            try
            {
                _Partner.Birthday = Convert.ToDateTime(txtBirthday.Text, new CultureInfo("vi-VN"));
            }
            catch
            {
                Message.Alert(Page, "Ngày sinh nhập không đúng!");
                return;
            }
        }
        _Partner.FullNameEn = Partner.UCS2Lower(_Partner.FullName);

        _Partner.Update();

        Response.Redirect(Constant.ADMIN_PATH + Resources.Url.PartnerList);
    }

    protected void btDelete_Click(object sender, EventArgs e)
    {
        Response.Redirect(Constant.ADMIN_PATH + Resources.Url.PartnerList);
    }

    protected void btCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Constant.ADMIN_PATH + Resources.Url.PartnerList);
    }
}
